package be.euroindicator.gui;

import java.io.File;
import java.util.Vector;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import be.euroindicator.Constants;
import be.euroindicator.logic.Country;

public class DynamicBubblesTab extends JSTab 
{
	//Logger
	private static Logger log=Logger.getLogger(new Throwable().getStackTrace()[1].getClassName());

	public DynamicBubblesTab(String title) 
	{
		super(title);
	}
	
	public void generateJS(String axeX,String axeY,Vector<Integer> years,Vector<Country> countries,Double data[][][],Double[][] correlations)
	{
		appendContent("var data="+generateData(axeX,axeY,years, countries, data, correlations)+";\n");
		appendContent("tab.dynamicPanel=new DynamicBubblesPanel(tab,data);");
	}
	
	private String generateData(String axeX,String axeY,Vector<Integer> years,Vector<Country> countries,Double data[][][],Double[][] correlations)
	{
		try
		{
			JSONObject resultJson=new JSONObject();
			
			//Years
			JSONArray yearsJson=new JSONArray();
			for (Integer year:years)
				yearsJson.put(year);
			resultJson.put(Constants.JSON_YEARS, yearsJson);
			
			double maxX=-Double.MIN_VALUE;
			double minX=Double.MAX_VALUE;
			double maxY=-Double.MIN_VALUE;
			double minY=Double.MAX_VALUE;
			
			//Data
			JSONArray dataJson=new JSONArray();
			for (int i=0;i<countries.size();i++)
			{
				Country country=countries.get(i);
				JSONObject countryJson=new JSONObject();
				countryJson.put(Constants.JSON_COUNTRY, country.getName());
				JSONArray countryDataJson=new JSONArray();
				for (int j=0;j<years.size();j++)
				{					
					JSONObject coordinatesJson=new JSONObject();
					coordinatesJson.put(Constants.JSON_X, data[j][i][0]);
					coordinatesJson.put(Constants.JSON_Y, data[j][i][1]);					
					countryDataJson.put(coordinatesJson);
				
					if (data[j][i][0]==null||data[j][i][1]==null)
						continue;
					
					if (data[j][i][0]>maxX)
						maxX=data[j][i][0];
					if (data[j][i][0]<minX)
						minX=data[j][i][0];
					if (data[j][i][1]>maxY)
						maxY=data[j][i][1];
					if (data[j][i][1]<minY)
						minY=data[j][i][1];
				}
								
				countryJson.put(Constants.JSON_POPULATION,country.getPopulation());
								
				countryJson.put(Constants.JSON_DATA, countryDataJson);
				countryJson.put(Constants.JSON_COLOR, country.getColor());
				dataJson.put(countryJson);
			}
						
			resultJson.put(Constants.JSON_DATA, dataJson);
			
			//x and y
			JSONObject xJson=new JSONObject();
			xJson.put(Constants.JSON_MAX, maxX);
			xJson.put(Constants.JSON_MIN, minX);
			xJson.put(Constants.JSON_NAME,axeX);
			resultJson.put(Constants.JSON_X, xJson);

			JSONObject yJson=new JSONObject();
			yJson.put(Constants.JSON_MAX, maxY);
			yJson.put(Constants.JSON_MIN, minY);
			yJson.put(Constants.JSON_NAME,axeY);
			resultJson.put(Constants.JSON_Y, yJson);
			
			//Correlations
			if (correlations!=null)
			{
				JSONArray correlationsJson=new JSONArray();
				for (int i=0;i<years.size();i++)
				{
					JSONObject correlationJson=new JSONObject();
					correlationJson.put(Constants.JSON_SLOPE,correlations[i][0]);
					correlationJson.put(Constants.JSON_OFFSET,correlations[i][1]);
					correlationJson.put(Constants.JSON_R,correlations[i][2]);					
					correlationsJson.put(correlationJson);
				}
			
				resultJson.put(Constants.JSON_CORRELATIONS,correlationsJson);
			}
			
			return resultJson.toString(1).replaceAll("\"", "'");
		}
		catch (JSONException e)
		{
			log.error("Error during the generation of the json string: "+e.getMessage());
			return null;
		}
	}
}
